﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Objects;

using MINERP.COM;

namespace MINERP.DAL.CustomerManage
{
    public class WorkerOrderFlowListDAL :DALBase
    {
        /// <summary>
        /// 得到产品的生产流程
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public static List<WorkOrderFlowList> GetOrderFlowListByOrderInfoID(int infoid)
        {
            List<WorkOrderFlowList> collection = new List<WorkOrderFlowList>();
            ObjectQuery<WorkOrderFlowList> workOrderFlows = GetWorkFlowListByOrderInfoID(infoid);

            foreach (var workOrderFlow in workOrderFlows)
            {
                collection.Add(workOrderFlow);
            }

            return collection;
        }
        /// <summary>
        /// 得到产品的生产流程(备份)
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public static List<WorkOrderFlowList_BK> GetOrderFlowListByOrderInfoID_bk(int infoid)
        {
            List<WorkOrderFlowList_BK> collection = new List<WorkOrderFlowList_BK>();
            ObjectQuery<WorkOrderFlowList_BK> workOrderFlows = GetWorkFlowListByOrderInfoID_bk(infoid);

            foreach (var workOrderFlow in workOrderFlows)
            {
                collection.Add(workOrderFlow);
            }

            return collection;
        }

        /// <summary>
        /// 得到订单的所有流程s
        /// </summary>
        /// <param name="bagID"></param>
        /// <returns></returns>
        public static List<WorkOrderFlowList> GetOrderFlowListByBagID(int bagID)
        {
            List<WorkOrderFlowList> collection = new List<WorkOrderFlowList>();

            List<int> list = OrderInfoDAL.GetOrderInfoIDByBagID(bagID);

            foreach (int orderInfoID in list)
            {
                ObjectQuery<WorkOrderFlowList> workOrderFlows = GetWorkFlowListByOrderInfoID(orderInfoID);

                foreach (var workOrderFlow in workOrderFlows)
                {
                    collection.Add(workOrderFlow);
                }
            }

            return collection;
        }

        public static string GetUserNameByUserID(int userID)
        {
            ObjectQuery<string> names = DataEntites.CreateQuery<string>(string.Format("SELECT VALUE it.UserName FROM users as it WHERE it.UserID = {0}", userID));

            if (names.Count() > 0)
                return names.First();

            return string.Empty;
        }

        /// <summary>
        /// 判断用户是否在生产此产品
        /// </summary>
        /// <param name="userid"></param>
        /// <param name="orderproductid"></param>
        /// <returns></returns>
        public static bool IsUserProduct(int userid, int orderproductid)
        {
            ObjectQuery<string> names = DataEntites.CreateQuery<string>(string.Format("SELECT VALUE it.tb_Tech.TechName FROM WorkOrderFlowList as it WHERE it.UserId = {0} AND it.orderInfo.OrderProductId = {1}", userid, orderproductid));

            if (names.Count() > 0)
                return true;

            return false;
        }

        /// <summary>
        /// 得到用户的id通过用户的名称
        /// </summary>
        /// <param name="userName"></param>
        /// <returns></returns>
        public static int GetUserIDByUserName(string userName)
        {
            ObjectQuery<int> names = DataEntites.CreateQuery<int>(string.Format("SELECT VALUE it.UserID FROM users as it WHERE it.UserName = {0}", "'"+userName + "'"));

            if (names.Count() > 0)
                return names.First();

            return 0;
        }

        private static ObjectQuery<WorkOrderFlowList> GetWorkFlowListByOrderInfoID(int orderInfoID)
        {
            ObjectQuery<WorkOrderFlowList> flowLists = DataEntites.WorkOrderFlowList.Where(string.Format("it.orderInfo.OrderProductId = {0}", orderInfoID)).Include("tb_Tech").OrderBy("it.WorkOrder");
            DataEntites.Refresh(RefreshMode.StoreWins, flowLists);
            return flowLists;
        }
        private static ObjectQuery<WorkOrderFlowList_BK> GetWorkFlowListByOrderInfoID_bk(int orderInfoID)       //备份
        {
            ObjectQuery<WorkOrderFlowList_BK> flowLists = DataEntites.WorkOrderFlowList_BK.Where(string.Format("it.orderInfo_BK.OrderProductId = {0}", orderInfoID)).Include("tb_Tech").OrderBy("it.WorkOrder");
            DataEntites.Refresh(RefreshMode.StoreWins, flowLists);
            return flowLists;
        }

        /// <summary>
        /// 根据产品的ID得到产品的预估完成时间
        /// </summary>
        /// <param name="orderproductid"></param>
        /// <returns></returns>
        public static DateTime GetOrderInfoPreCompletetime(int orderproductid)
        {
           
            ObjectQuery<DateTime> selectTime = DataEntites.CreateQuery<DateTime>(string.Format("SELECT VALUE it.PreCompleteTime FROM WorkOrderFlowList as it WHERE it.orderInfo.OrderProductId = {0}", orderproductid));

            try
            {
                if (selectTime.Count() > 0)
                {
                    return selectTime.Max();
                }
            }
            catch
            {
                return DateTime.MinValue;
            }

            return DateTime.MinValue;
        }
        /// <summary>
        /// 根据产品的ID得到产品的预估完成时间(备份)
        /// </summary>
        /// <param name="orderproductid"></param>
        /// <returns></returns>
        public static DateTime GetOrderInfoPreCompletetime_bk(int orderproductid)
        {

            ObjectQuery<DateTime> selectTime = DataEntites.CreateQuery<DateTime>(string.Format("SELECT VALUE it.PreCompleteTime FROM WorkOrderFlowList_bk as it WHERE it.orderInfo_bk.OrderProductId = {0}", orderproductid));

            try
            {
                if (selectTime.Count() > 0)
                {
                    return selectTime.Max();
                }
            }
            catch
            {
                return DateTime.MinValue;
            }

            return DateTime.MinValue;
        }

        /// <summary>
        /// 得到产品正在生产的流程名称
        /// </summary>
        /// <param name="orderproductid"></param>
        /// <returns></returns>
        public static string GetFlowStatusNameByOrderProduct(int orderproductid)
        {
            int status = (int)OrderFlowListStatus.UseableAccept;
            int statusUse = (int)OrderFlowListStatus.Useable;
            int statusStop = (int)OrderFlowListStatus.UnProduct;

            using (newSampleYPDBEntities db=new newSampleYPDBEntities ())
            {
              WorkOrderFlowList curWl=  db.WorkOrderFlowList.Where(w => w.orderInfo.OrderProductId == orderproductid && (w.Status == status || w.Status == statusUse || w.Status == statusStop)).FirstOrDefault();
              if (curWl==null)
              {
                  //从备份表中查找
                  return string.Empty                      ;
              }
              else
              {
                  curWl.tb_TechReference.Load();
                  return curWl.tb_Tech.TechName;
              }
            }
            //ObjectQuery<string> techName = DataEntites.CreateQuery<string>(string.Format("SELECT VALUE it.tb_Tech.TechName FROM WorkOrderFlowList as it WHERE it.orderInfo.OrderProductId = {0} AND (it.Status = {1} or it.Status = {2} or it.Status={3})", orderproductid, status, statusUse,statusStop));

            //foreach (string name in techName)
            //{
            //    return name;
            //}

            //return string.Empty;


        }

        /// <summary>
        /// 判断当前产品是否在当前生产状态
        /// </summary>
        /// <param name="orderproductid"></param>
        /// <param name="flowStatus"></param>
        /// <returns></returns>
        public static bool GetFlowStatusNameByOrderProduct(int orderproductid, int flowStatus)
        {
            int status = (int)OrderFlowListStatus.UseableAccept;    //在非实物中北接收
            int status2 = (int)OrderFlowListStatus.Useable;         //可操作
            ObjectQuery<string> techName = DataEntites.CreateQuery<string>(string.Format("SELECT VALUE it.tb_Tech.TechName FROM WorkOrderFlowList as it WHERE it.orderInfo.OrderProductId = {0} AND (it.Status = {1} or it.Status={2}) AND it.tb_Tech.TechId = {3}", orderproductid, status,status2,flowStatus));

            foreach (string name in techName)
            {
                return true;
            }

            return false;
        }

        /// <summary>
        /// 得到产品的生产状态
        /// </summary>
        /// <param name="infoid"></param>
        /// <returns></returns>
        public static bool GetOrderInfoStatusByOrderInfo(int infoid)
        {
            int status = (int)OrderFlowListStatus.UnProduct;
            ObjectQuery<string> techNames = DataEntites.CreateQuery<string>("SELECT VALUE it.tb_Tech.TechName FROM WorkOrderFlowList as it WHERE " + string.Format("it.orderInfo.OrderProductId = {0} AND it.Status = {1}", infoid, status));

            if (techNames.Count() > 0)
            {
                foreach (var techName in techNames)
                {
                    switch (techName)
                    {
                        case "开始":
                            return true;
                        case "修片":
                            return true;
                        case "调色":
                            return true;
                        case "设计":
                            return true;
                        case "拼版":
                            return true;
                        case "打印":
                            return true;
                        case "分片":
                            return true;
                        case "淋膜排序":
                            return true;
                        case "包装":
                            return true;
                        case "配送":
                            return true;
                        case "委外":
                            return true;
                        case "完成（结点）":
                            return true;

                        default:
                            return false;

                    }
                }

                return false;
            }

            return false;
        }

        /// <summary>
        /// 得到所有的节点信息
        /// </summary>
        /// <returns></returns>
        public static List<TechInfo> GetAllTechName()
        {
            List<TechInfo> collection = new List<TechInfo>();

            TechInfo hold = new TechInfo();
            hold.TechID = -1;
            hold.TechName = string.Empty;
            collection.Add(hold);
            foreach (var item in DataEntites.tb_Tech)
            {
                TechInfo tech = new TechInfo();

                tech.TechID = item.TechId;
                tech.TechName = item.TechName;
                collection.Add(tech);
            }

            return collection;
        }
        /// <summary>
        /// 取得产品
        /// </summary>
        /// <param name="inproductId"></param>
        /// <returns></returns>
        public orderInfo GetProductById(int inproductId)
        {
            return DataEntites.orderInfo.Where(p => p.OrderProductId == inproductId).FirstOrDefault();
        }
        /// <summary>
        /// 取得正在生产的返单产品
        /// </summary>
        /// <param name="inProductId"></param>
        /// <returns></returns>
        //public InnerReturnOrder GetInnerById(int inProductId)
        //{
        //    int status = (int) InnerReturnOrderStatus.Use;
        //    return
        //        DataEntites.InnerReturnOrder.Where(p => p.orderInfo.OrderProductId == inProductId && p.status == status)
        //            .FirstOrDefault();
        //}
        /// <summary>
        /// 所有返单产品是否完成 09-14 szl 
        /// </summary>
        /// <param name="inProductId"></param>
        
        public bool IsAllComplete(int inProductID)
        {
            using (newSampleYPDBEntities dataBase = new newSampleYPDBEntities())
            {
                int status = (int)OrderInfoStatus.Doing;
                int count = dataBase.orderInfo.Where(p => p.ParentProductId == inProductID && p.status == status).Count();
                if (count > 0)
                    return false;
                else
                    return true;
            }         
           
        }
    }
}
